package org.hadoop.seq;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * 读取SequenceFile将它转存成文本文件
 *
 * @author wangjian
 */
public class ReadSequenceFileMR extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        Configuration config = getConf();
        Path dest = new Path("D:/a/out001");
        FileSystem fs = FileSystem.get(config);
        if (fs.exists(dest)) {
            fs.delete(dest, true);
        }
        //声明Job
        Job job = Job.getInstance(config, "Seq");
        job.setJarByClass(getClass());
        //设置Mapper
        job.setMapperClass(Demo10Mapper.class);
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(Text.class);
        //设置Reducer
        job.setReducerClass(Demo10Reducer.class);
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(Text.class);
        //设置输入流为SequenceFile
        job.setInputFormatClass(SequenceFileInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        //设置输入输出目录
        FileInputFormat.setInputPaths(job, new Path("D:/a/1.seq"));
        FileOutputFormat.setOutputPath(job, dest);
        int code = job.waitForCompletion(true) ? 0 : 1;
        return code;
    }

    public static class Demo10Mapper
            extends Mapper<IntWritable, Text, IntWritable, Text> {
        @Override
        public void map(IntWritable key, Text value, Mapper<IntWritable, Text, IntWritable, Text>.Context context)
                throws IOException, InterruptedException {
            context.write(key, value);
        }
    }
    public static class Demo10Reducer
            extends Reducer<IntWritable, Text, IntWritable, Text> {
        @Override
        protected void reduce(IntWritable key, Iterable<Text> values,
                              Reducer<IntWritable, Text, IntWritable, Text>.Context context)
                throws IOException, InterruptedException {
            context.write(key, values.iterator().next());
        }
    }
    public static void main(String[] args) throws Exception {
        int code = ToolRunner.run(new ReadSequenceFileMR(), args);
        System.exit(code);
    }
}
